<?php include('includes/array.in.php');?>
<div class="page">
	<div class="menu">
		<?php include('modules/members/dashboard.php');?>
    </div>
	<div class="products-list" style="overflow:hidden;">
    <?php
        if($_SESSION['user'] != "superadmin"){
            echo '<h2 align="center" style="color:red; margin-top: 10%;">เข้าดูส่วนนี้ได้เฉพาะผู้ทีมีสิทธิ์ระดับ Superadmin เท่านั้น</h2>';
            die();
        }
    ?>
    <div class="setSize" style="width:300px;margin:0 auto;">
        <div id="datepicker"></div>
        <div class="clear"></div>
        <br/>
        <div id="report">
        <?php
        	$sumAllPrice = 0;
			$discount = 0;
			$sumCost = 0;						
			$month = $FULL_MONTH[date('n',time())];
									
			$sql = 'SELECT * FROM stock WHERE stock_date = "'.date('dmY',time()).'"';
			$q = mysql_query($sql)or die(mysql_error());
			
			while($rs = mysql_fetch_assoc($q)){
				$sumAllPrice += ($rs['price'] * $rs['all_sale']);				
				$discount	 += ($rs['bonus'] * $rs['price']);				
				$sumCost	 += $rs['cost'] * $rs['all_sale'];
			}
		?>	
            <table border="1" cellspacing="2" cellpadding="2" width="300" align="center">
              <tr>
                <td colspan="2"><strong> สรุปยอดประจำเดือน <span class="month" style="color:blue;"><?php echo $month; ?></span> </strong></td>
              </tr>
              <tr>
                <td>ต้นทุนรวม</td>
                <td align="right"><span style="color:red;"><?php echo number_format($sumCost,2,'.',',');?>฿</span></td>
              </tr>
              <tr>
                <td>ขายได้รวม</td>
                <td align="right"><span style="color:red;"><?php echo number_format($sumAllPrice,2,'.',',');?>฿</span></td>
              </tr>
              <tr>
                <td>ขายได้สุทธิ(ลบราคาของแถมแล้ว)</td>
                <td align="right"><span style="color:red;"><?php echo number_format(($sumAllPrice-$discount),2,'.',',');?>฿</span></td>
              </tr>
              <tr>
                <td>กำไรสุทธิ(ขายได้สุทธิ - ต้นทุน)</td>
                <td align="right"><span style="color:red;"><?php echo number_format(($sumAllPrice-$discount)-$sumCost,2,'.',',');?>฿</span></td>
              </tr>
            </table>
        </div>
    </div>
    <div class="clear"></div>
   	<div class="stock_list">   
    <?php    	
	$sql = 'SELECT
				`units`.`name`
				, `stock`.*
			FROM
				`hkpacth_store`.`units`
				INNER JOIN `hkpacth_store`.`stock` 
					ON (`units`.`id` = `stock`.`unit`) 
			WHERE stock_date = "'.date('dmY',time()).'"';
	$q = mysql_query($sql)or die(mysql_error());	
	$c = mysql_num_rows($q);		
	
	if($c>0){
	$msg .= '
	<h2>สต๊อกสินค้าวันที่ <span class="date" style="color:blue;">'.ThaiTimeConvert(time(),0,0).'</span></h2>
	<div class="clear"></div>
	<table cellspacing="0" cellpadding="0" border="1">
	<thead>
	  <tr>
		<td width="1" rowspan="2" align="center"><strong>ลำดับ</strong></td>
		<td width="174" rowspan="2" align="center"><strong>รายการสินค้า</strong></td>
		<td width="83" rowspan="2" align="center"><strong>ราคา/หน่วย</strong></td>
		<td width="52" rowspan="2" align="center"><strong>หน่วย</strong></td>
		<td width="69" rowspan="2" align="center"><strong>ยกมา</strong></td>
		<td width="67" rowspan="2" align="center"><strong>รับ</strong></td>
		<td width="58" rowspan="2" align="center"><strong>รวม</strong></td>
		<td colspan="2" align="center"><strong>จ่าย</strong></td>
		<td width="95" rowspan="2" align="center"><strong>จ่ายทั้งหมด</strong></td>
		<td width="67" rowspan="2" align="center"><strong>คงเหลือ</strong></td>
		<td width="74" rowspan="2" align="center"><strong>หมายเหตุ</strong></td>
	</tr>
	  <tr>
		<td width="70" align="center"><strong>จ่ายลูกค้า</strong></td>
		<td width="60" align="center"><strong>แถม</strong></td>
	</tr>
	</thead>
	<tbody>';
	$i=1; while($rs	 = mysql_fetch_assoc($q)){
	$msg .= '
	  <tr class="item-row">
		<td align="center">'.$i++.'</td>
		<td align="center">
			<div class="product-item">
				'.$rs['product_name'].'                    
			</div>
		</td>
		<td align="center">
			<div class="unit_price" data-price="'.$rs['price'].'">
				'.$rs['price'].'/'.$rs['name'].'
			</div>
		</td>
		<td align="center">
			<div class="unit_name">
				'.$rs['name'].'
			</div>
		</td>
		<td align="center">
			<div class="bring">
				'.$rs['bring'].'
			</div>
		</td>
		<td align="center">
			<div class="product-receive">
				'.$rs['receive'].'
			</div>
		</td>
		<td align="center">
			<div class="sum" field="sum">'.$rs['sum'].'</div>
		</td>
		<td align="center">
			<div class="product-pay-off">
				'.$rs['sale'].'
			</div>
		</td>
		<td align="center">
			<div class="product-bonus">
				'.$rs['bonus'].'
			</div>
		</td>
		<td align="center">
			<div class="all_sale" field="all_sale">'.$rs['all_sale'].'</div>
		</td>
		<td align="center">
			<div class="total" field="total">'.$rs['total'].'</div>
		</td>
		<td align="center">
			<div class="product-ann">
				'.(($rs['annotation']!="")?'<img src="css/images/info.gif" title="'.$rs['annotation'].'">"':'').'
			</div>
		</td>
	  </tr>';
	  }
  	$msg .= '        
	</tbody>
	</table>
	';
	echo $msg;
	}else{
		echo '<h1 style="margin-top:25px;text-align:center;font-size:40px;color:red;">วันนี้ไม่มีการทำรายการ</h1>';
	}
    ?>
    </div>
    </div>
</div>
<script type="text/javascript" src="js/jquery-ui-1.8.10.offset.datepicker.min.js"></script>
<script type="text/javascript" src="js/jquery.ui.datepicker-th.js"></script>
<script type="text/javascript">
	function updateAllPrice(){
		var sumAllPrice = 0;
		$('.all_sale').each(function(){
			sumAllPrice += (parseInt($(this).text()) * parseInt($(this).closest('tr').find('.unit_price').attr('data-price')))
		});
		$('.total_price').html(addCommas(roundNumber(sumAllPrice,2))+"฿");
	}

	$(function() {
		var d = new Date();
		var day = d.getDay();
		var toDay = d.getDate() + '/' + (d.getMonth() + 1) + '/' + (d.getFullYear() + 543);
		
		$( "#datepicker" ).datepicker({
			dateFormat: 'mm/dd/yy', 
			isBuddhist: true, 
			defaultDate: toDay,
			showButtonPanel: true,			
			changeMonth: true,
			changeYear: true,
			showOtherMonths: true,
			selectOtherMonths: true,
			changeMonth: true,
			onChangeMonthYear: function(year, month, inst) { 
				$.ajax({
					type	: 'POST',
					url		: 'query.php',
					data	: {stock_report:true, date:month+'/'+day+'/'+(year+543)},
					dataType: 'html',
					success	: function(result){
						$('.stock_list').html(result);

						$.ajax({
							type	: 'POST',
							url		: 'query.php',
							data	: {money_report:true, date2:month+'/'+day+'/'+(year+543), result:result},
							dataType: 'html',
							success	: function(report){
								$('#report').html(report);								
							}
						});		
									
					}
				});
			},
			onSelect: function (value, date) {
				//alert(value);
				//location.href='?m=products&p=stock_reports&d='+value;	
				$.ajax({
					type	: 'POST',
					url		: 'query.php',
					data	: {stock_report:true, date:value},
					dataType: 'html',
					success	: function(result){
						$('.stock_list').html(result);
						
						$.ajax({
							type	: 'POST',
							url		: 'query.php',
							data	: {money_report:true, date2:value, result:result},
							dataType: 'html',
							success	: function(report){
								$('#report').html(report);								
							}
						});		
									
					}
				});			
			}
		},$.datepicker.regional[ "th" ]);
		
		updateAllPrice();

		
		
	});
</script>
<style type="text/css">
#datepicker {
	text-shadow: none;
	margin: 0 auto;
	width: 19.2em;
}
table {
    border-collapse: collapse;
}
tbody > tr {
	background-color: #fff;
}
</style>